// 该文件专门用于创建整个应用的路由器

import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import {userRoute} from "./userRoute";
import {gameRoute} from "./gameRoute";
import {indexRoute} from "./indexRoute";
import {discoveryRoute} from "./discoveryRoute";
import {aboutRoute} from "./aboutRoute";
import {StoreLoginState} from "../store/userInfo";

const baseRoute: Array<RouteRecordRaw> = [{
    path: "/",
    redirect: "/index"
}, {
    path: "/login",
    name: "/login",
    meta: {
        title: "登录",
        isShow: true,
    },
    component: () => import('../components/user/Login.vue'),

},]
const routes: Array<RouteRecordRaw> = [...userRoute, ...baseRoute,
    ...aboutRoute, ...indexRoute, ...discoveryRoute, ...gameRoute
];


const router = createRouter({
    history: createWebHashHistory(),
    routes: routes
})

router.beforeEach((to, from, next) => {
        console.log(to.fullPath, StoreLoginState().Get())
        if (StoreLoginState().Get()) { //   已经登录成功
            next()
        } else {
            // 未登录 如果访问 user 相关 就前往登录
            if (to.fullPath.includes("user") || to.fullPath.includes("discovery")) {
                // console.log(to.fullPath)
                next({path: '/login'})
            } else { //非用户相关
                next()
            }
        }
        document.title = to.meta.title ? to.meta.title.toString() : "EmpShop"

    }
)


export default router
